﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//定义用户表格控件
Ext.define
(
    'MaintainPlan.Grid',
    {
        extend: 'Ext.grid.Panel',

        //编辑窗体
        win_edit: null,

        //新增窗体
        win_create: null,

        //计划新增窗体
        win_plan_create: null,

        //计划编辑窗体
        win_plan_edit: null,

        //计划编辑窗体
        win_plan_edit: null,

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '-',
                                {
                                    iconCls: 'icon-add',
                                    text: '新增',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '新增养护计划',
                                    handler: this.onAddClick
                                },
                                {
                                    iconCls: 'icon-delete',
                                    text: '删除',
                                    disabled: true,
                                    hidden: false,
                                    itemId: 'button-delete',
                                    scope: this,
                                    tooltip: '删除养护计划',
                                    handler: this.onDeleteClick
                                },
                                {
                                    iconCls: 'icon-edit',
                                    text: '编辑',
                                    itemId: 'button-edit',
                                    disabled: true,
                                    hidden: false,
                                    scope: this,
                                    tooltip: '编辑养护计划',
                                    handler: this.onEditClick
                                },
                                {
                                    iconCls: 'icon-view',
                                    itemId: 'button-view',
                                    text: '查看',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '查看养护计划',
                                    handler: this.onViewClick
                                },
                                '->',
                                {
                                    iconCls: 'icon-submit',
                                    text: '提交审核',
                                    itemId: 'button-submit',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '提交审核',
                                    handler: this.onSubmitClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中养护变化事件
        onSelectionChange: function (selectionModel, selections)
        {
            //判断是否选中养护
            if (selections.length === 0)
            {
                //禁用删除按钮
                this.down('#button-delete').setDisabled(true);
                //禁用编辑按钮
                this.down('#button-edit').setDisabled(true);
                //禁用上传按钮
                this.down('#button-submit').setDisabled(true);
                //禁用查看按钮
                this.down('#button-view').setDisabled(true);
            }
            else
            {
                //启用删除按钮
                this.down('#button-delete').setDisabled(false);
                //启用编辑按钮
                this.down('#button-edit').setDisabled(false);
                //启用上传按钮
                this.down('#button-submit').setDisabled(false);
                //启用查看按钮
                this.down('#button-view').setDisabled(false);
            }
        },

        //处理查看按钮点击事件
        onViewClick: function ()
        {
            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {
                //查看工程详细信息
                window.open(Common.MakeAbsoluteUrl('ViewPlan?') + 'planID=' + selection[0].data.PlanID);
            }
        },

        //处理删除按钮点击事件
        onDeleteClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //询问是否确定删除
                Ext.Msg.show
                (
                    {
                        title: '删除养护计划',
                        width: 320,
                        height: 240,
                        msg: '确定要删除选中的养护计划吗?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('Delete'),
                                        method: HttpMethod.Post,
                                        params: selection.data,
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //刷新数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //刷新数据
            this.store.load();
        },

        //新增计划
        onAppendPlanItem: function (owner)
        {
            //判断新增窗体是否创建
            if (!this.win_plan_create)
            {
                //创建窗体
                this.win_plan_create = new Ext.Window
                (
                    {
                        title: '新增养护计划',
                        maximizable: false,
                        maximized: false,
                        width: 480,
                        height: 240,
                        bodyBorder: false,
                        draggable: true,
                        isTopContainer: true,
                        modal: true,
                        resizable: false,
                        bodyPadding: 0,
                        closeAction: 'hide',
                        closable: false,
                        layout: 'fit',
                        fieldDefaults:
                        {
                            labelAlign: 'left',
                            labelWidth: 72,
                            anchor: '100%'
                        },
                        items:
                        [
                            {
                                xtype: 'form',
                                method: 'POST',
                                waitMsgTarget: true,
                                waitTitle: Common.ApplicationName,
                                frame: false,
                                bodyPadding: 12,
                                bodyStyle: 'background-color:White;margin:0;',
                                bodyBorder: false,
                                border: 0,
                                autoScroll: true,
                                defaultType: 'textfield',
                                autoScroll: false,
                                fieldDefaults:
                                {
                                    labelAlign: 'left',
                                    labelWidth: 72,
                                    anchor: '100%',
                                    msgTarget: 'qtip',
                                    autoFitErrors: true,
                                    disabledCls: ''
                                },
                                items:
                                [
                                    {
                                        xtype: 'combobox',
                                        name: 'CategoryID',
                                        itemId: 'cboCategory',
                                        fieldLabel: '养护类型',
                                        emptyText: '养护类型',
                                        store: store_master_category,
                                        displayField: 'Name',
                                        valueField: 'ID',
                                        queryMode: 'local',
                                        hideTrigger: false,
                                        forceSelection: true,
                                        typeAhead: true,
                                        autocomplete: true,
                                        allowBlank: false,
                                        listeners:
                                        {
                                            select: function (field, records, eOpts)
                                            {
                                                //设置结果
                                                field.up('form').down('#txtUnit').setValue(records[0].data.Unit);

                                                //记录当前选择的记录
                                                field.current_row = records[0];
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        disabled: true,
                                        itemId: 'txtUnit',
                                        fieldLabel: '计量单位',
                                        emptyText: '工作量计量单位'
                                    },
                                    {
                                        xtype: 'numberfield',
                                        name: 'Quantity',
                                        itemId: 'txtQuantity',
                                        fieldLabel: '工作量',
                                        emptyText: '养护计划',
                                        allowBlank: false,
                                        minValue: 0,
                                        decimalPrecision: 3,
                                        listeners:
                                        {
                                            change: function (field, newValue, oldValue, options)
                                            {
                                                //获得单价
                                                var price = field.up('form').down('#txtPrice').getValue();

                                                //判断是否为数据
                                                if (!isNaN(price) && !isNaN(newValue))
                                                {
                                                    //计算结果
                                                    var value = Ext.util.Format.number(price * newValue, '0,000.000');

                                                    //设置结果
                                                    field.up('form').down('#txtAmount').setValue(value);
                                                }
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'numberfield',
                                        name: 'Price',
                                        itemId: 'txtPrice',
                                        fieldLabel: '估算单价',
                                        emptyText: '工程量单价估算',
                                        allowBlank: false,
                                        minValue: 0,
                                        decimalPrecision: 3,
                                        listeners:
                                        {
                                            change: function (field, newValue, oldValue, options)
                                            {
                                                //获得工作量
                                                var quantity = field.up('form').down('#txtQuantity').getValue();

                                                //判断是否为数据
                                                if (!isNaN(quantity) && !isNaN(newValue))
                                                {
                                                    //计算结果
                                                    var value = Ext.util.Format.number(quantity * newValue, '0,000.000');

                                                    //设置结果
                                                    field.up('form').down('#txtAmount').setValue(value);
                                                }
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        itemId: 'txtAmount',
                                        fieldLabel: '小　　计',
                                        emptyText: '小计',
                                        disabled: true
                                    }
                                ],
                                buttons:
                                [
                                    {
                                        text: '确定',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up('form');

                                            //获得窗体
                                            var window = this.up('window');

                                            //获得表单
                                            var formBase = form.getForm();

                                            //获得控件
                                            var cboCategory = form.down('#cboCategory');

                                            //获得分类
                                            var categoryID = cboCategory.getValue();

                                            //用于保存分类
                                            var category = null;

                                            //获得分类
                                            if (cboCategory.current_row)
                                            {
                                                //获得分类
                                                category = cboCategory.current_row.data.Name;
                                            }

                                            //检索记录
                                            if (store_plans.find('CategoryID', categoryID) != -1)
                                            {
                                                //提示错误信息
                                                Ext.MessageBox.show
                                                (
                                                    {
                                                        title: Common.ApplicationName,
                                                        msg: '已经为养护类型“' + category + '”创建了养护计划，不允许重复添加。',
                                                        buttons: Ext.Msg.OK,
                                                        icon: Ext.Msg.ERROR
                                                    }
                                                );

                                                //直接返回
                                                return;
                                            }

                                            //检查数据是否合法
                                            if (formBase.isValid())
                                            {
                                                //获得单价
                                                var price = form.down('#txtPrice').getValue();

                                                //获得工作量
                                                var quantity = form.down('#txtQuantity').getValue();

                                                //获得单位
                                                var unit = form.down('#txtUnit').getValue();

                                                //获得合计
                                                var amount = form.down('#txtAmount').getValue();

                                                //创建实体
                                                var entity = Ext.create
                                                (
                                                    'MaintainPlanItemModel',
                                                    {
                                                        PlanItemID: categoryID,
                                                        PlanID: categoryID,
                                                        CategoryID: categoryID,
                                                        Category: category,
                                                        Quantity: quantity,
                                                        Unit: unit,
                                                        Price: price,
                                                        Amount: amount
                                                    }
                                                );

                                                //新增数据
                                                store_plans.add(entity);

                                                //刷新合计
                                                if (window.current_owner)
                                                {
                                                    window.current_owner.refreshSummary();
                                                }

                                                //重置数据
                                                formBase.reset();

                                                //隐藏窗体
                                                window.hide();
                                            }
                                        }
                                    },
                                    {
                                        text: '取消',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up('form').getForm();
                                            //重置数据
                                            form.reset();
                                            //隐藏窗体
                                            this.up('window').hide();
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                 );
            }

            //设置所属
            this.win_plan_create.current_owner = owner;

            //显示窗体
            this.win_plan_create.show();
        },

        //编辑计划
        onEditPlanItem: function (owner, record)
        {
            //判断新增窗体是否创建
            if (!this.win_plan_edit)
            {
                //创建窗体
                this.win_plan_edit = new Ext.Window
                (
                    {
                        title: '编辑养护计划',
                        maximizable: false,
                        maximized: false,
                        width: 480,
                        height: 240,
                        bodyBorder: false,
                        draggable: true,
                        isTopContainer: true,
                        modal: true,
                        resizable: false,
                        bodyPadding: 0,
                        closeAction: 'hide',
                        closable: false,
                        layout: 'fit',
                        fieldDefaults:
                        {
                            labelAlign: 'left',
                            labelWidth: 72,
                            anchor: '100%'
                        },
                        listeners:
                        {
                            //挂接显示事件
                            show: function (win, options)
                            {
                                //读取记录
                                var form = win.down('form');

                                //加载数据
                                form.loadRecord(win.current_row);
                            }
                        },
                        items:
                        [
                            {
                                xtype: 'form',
                                method: 'POST',
                                waitMsgTarget: true,
                                waitTitle: Common.ApplicationName,
                                frame: false,
                                bodyPadding: 12,
                                bodyStyle: 'background-color:White;margin:0;',
                                bodyBorder: false,
                                border: 0,
                                autoScroll: true,
                                defaultType: 'textfield',
                                autoScroll: false,
                                fieldDefaults:
                                {
                                    labelAlign: 'left',
                                    labelWidth: 72,
                                    anchor: '100%',
                                    msgTarget: 'qtip',
                                    autoFitErrors: true,
                                    disabledCls: ''
                                },
                                items:
                                [
                                    {
                                        xtype: 'combobox',
                                        name: 'CategoryID',
                                        itemId: 'cboCategory',
                                        fieldLabel: '养护类型',
                                        emptyText: '养护类型',
                                        store: store_master_category,
                                        displayField: 'Name',
                                        valueField: 'ID',
                                        queryMode: 'local',
                                        hideTrigger: false,
                                        forceSelection: true,
                                        typeAhead: true,
                                        autocomplete: true,
                                        allowBlank: false,
                                        listeners:
                                        {
                                            select: function (field, records, eOpts)
                                            {
                                                //设置结果
                                                field.up('form').down('#txtUnit').setValue(records[0].data.Unit);

                                                //记录当前选择的记录
                                                field.current_row = records[0];
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        readOnly: true,
                                        name: 'Unit',
                                        itemId: 'txtUnit',
                                        fieldLabel: '计量单位',
                                        emptyText: '工作量计量单位'
                                    },
                                    {
                                        xtype: 'numberfield',
                                        name: 'Quantity',
                                        itemId: 'txtQuantity',
                                        fieldLabel: '工作量',
                                        emptyText: '养护计划',
                                        allowBlank: false,
                                        minValue: 0,
                                        decimalPrecision: 3,
                                        listeners:
                                        {
                                            change: function (field, newValue, oldValue, options)
                                            {
                                                //获得单价
                                                var price = field.up('form').down('#txtPrice').getValue();

                                                //判断是否为数据
                                                if (!isNaN(price) && !isNaN(newValue))
                                                {
                                                    //计算结果
                                                    var value = Ext.util.Format.number(price * newValue, '0,000.000');

                                                    //设置结果
                                                    field.up('form').down('#txtAmount').setValue(value);
                                                }
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'numberfield',
                                        name: 'Price',
                                        itemId: 'txtPrice',
                                        fieldLabel: '估算单价',
                                        emptyText: '工程量单价估算',
                                        allowBlank: false,
                                        minValue: 0,
                                        decimalPrecision: 3,
                                        listeners:
                                        {
                                            change: function (field, newValue, oldValue, options)
                                            {
                                                //获得工作量
                                                var quantity = field.up('form').down('#txtQuantity').getValue();

                                                //判断是否为数据
                                                if (!isNaN(quantity) && !isNaN(newValue))
                                                {
                                                    //计算结果
                                                    var value = Ext.util.Format.number(quantity * newValue, '0,000.000');

                                                    //设置结果
                                                    field.up('form').down('#txtAmount').setValue(value);
                                                }
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        name: 'Amount',
                                        itemId: 'txtAmount',
                                        fieldLabel: '小　　计',
                                        emptyText: '小计',
                                        readOnly: true
                                    }
                                ],
                                buttons:
                                [
                                    {
                                        text: '确定',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up('form');

                                            //获得窗体
                                            var window = this.up('window');

                                            //获得表单
                                            var formBase = form.getForm();

                                            //获得控件
                                            var cboCategory = form.down('#cboCategory');

                                            //获得分类
                                            var categoryID = cboCategory.getValue();

                                            //判断分类是否发生变化
                                            if (window.current_row.data.CategoryID != categoryID)
                                            {
                                                //用于保存分类
                                                var category = null;

                                                //获得分类
                                                if (cboCategory.current_row)
                                                {
                                                    //获得分类
                                                    category = cboCategory.current_row.data.Name;
                                                }

                                                //检索记录
                                                if (store_plans.find('CategoryID', categoryID) != -1)
                                                {
                                                    //提示错误信息
                                                    Ext.MessageBox.show
                                                    (
                                                        {
                                                            title: Common.ApplicationName,
                                                            msg: '已经为养护类型“' + category + '”创建了养护计划，不允许重复添加。',
                                                            buttons: Ext.Msg.OK,
                                                            icon: Ext.Msg.ERROR
                                                        }
                                                    );

                                                    //直接返回
                                                    return;
                                                }
                                            }

                                            //检查数据是否合法
                                            if (formBase.isValid())
                                            {
                                                //更新数据
                                                formBase.updateRecord(window.current_row);

                                                //刷新合计
                                                if (window.current_owner)
                                                {
                                                    window.current_owner.refreshSummary();
                                                }

                                                //重置数据
                                                formBase.reset();

                                                //隐藏窗体
                                                window.hide();
                                            }
                                        }
                                    },
                                    {
                                        text: '取消',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up('form').getForm();
                                            //重置数据
                                            form.reset();
                                            //隐藏窗体
                                            this.up('window').hide();
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                 );
            }

            //设置所属
            this.win_plan_edit.current_owner = owner;

            //设置当前行
            this.win_plan_edit.current_row = record;

            //显示窗体
            this.win_plan_edit.show();
        },

        //处理新增按钮点击事件
        onAddClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得表格控件
            var maintainplan_grid = this;

            //判定窗体是否创建
            if (!this.win_create)
            {
                //创建数据表格
                var grid = Ext.create
                (
                    'Ext.grid.Panel',
                    {
                        store: store_plans,
                        flex: 1,
                        refreshSummary: function ()
                        {
                            try
                            {
                                //获得合计行
                                var txtSummary = this.down('#text-summary')
                                //计算合计
                                var summary = store_plans.sum('Amount', false);
                                //显示合计值
                                txtSummary.update('合计：' + Ext.util.Format.number(summary, '0,000.000'));
                            }
                            catch (e)
                            {
                                alert(e);
                            }
                        },
                        listeners:
                        {
                            //处理选择变化事件
                            selectionchange: function (electionModel, selections, eOpts)
                            {
                                //判断是否选中养护
                                if (selections.length === 0)
                                {
                                    //禁用删除按钮
                                    this.down('#button-delete').setDisabled(true);
                                    //禁用编辑按钮
                                    this.down('#button-edit').setDisabled(true);
                                }
                                else
                                {
                                    //启用删除按钮
                                    this.down('#button-delete').setDisabled(false);
                                    //启用编辑按钮
                                    this.down('#button-edit').setDisabled(false);
                                }
                            }
                        },
                        dockedItems:
                        [
                            {
                                xtype: 'toolbar',
                                dock: 'bottom',
                                border: 0,
                                height: 36,
                                items:
                                [
                                    {
                                        iconCls: 'icon-add',
                                        itemId: 'button-add',
                                        text: '新增',
                                        scope: this,
                                        hidden: false,
                                        tooltip: '新增养护计划',
                                        handler: function (button)
                                        {
                                            //获得表格控件
                                            var grid = button.up('grid');

                                            //新增记录
                                            maintainplan_grid.onAppendPlanItem(grid);
                                        }
                                    },
                                    {
                                        iconCls: 'icon-edit',
                                        itemId: 'button-edit',
                                        text: '编辑',
                                        scope: this,
                                        hidden: false,
                                        disabled: true,
                                        tooltip: '编辑养护计划',
                                        handler: function (button)
                                        {
                                            //获得表格控件
                                            var grid = button.up('grid');

                                            //获得选中的行
                                            var selection = grid.getView().getSelectionModel().getSelection();

                                            //判断是否选中行
                                            if (selection.length > 0)
                                            {
                                                //编辑记录
                                                maintainplan_grid.onEditPlanItem(grid, selection[0]);
                                            }
                                        }
                                    },
                                    {
                                        iconCls: 'icon-delete',
                                        itemId: 'button-delete',
                                        text: '删除',
                                        scope: this,
                                        hidden: false,
                                        disabled: true,
                                        tooltip: '删除养护计划',
                                        handler: function (button)
                                        {
                                            //获得表格控件
                                            var grid = button.up('grid');

                                            //获得选中的行
                                            var selection = grid.getView().getSelectionModel().getSelection();

                                            //判断是否选中行
                                            if (selection.length > 0)
                                            {
                                                //删除数据
                                                store_plans.remove(selection);
                                            }

                                            //刷新合计
                                            grid.refreshSummary();
                                        }
                                    },
                                    '->',
                                    {
                                        itemId: 'text-summary',
                                        xtype: 'tbtext',
                                        text: '合计：0.000'
                                    }
                                ]
                            }
                        ],
                        columns:
                        [
                            {
                                text: '养护类型',
                                width: 160,
                                flex: true,
                                sortable: false,
                                align: 'left',
                                dataIndex: 'Category',
                                hideable: false
                            },
                            {
                                text: '养护工程量',
                                width: 120,
                                sortable: false,
                                align: 'right',
                                dataIndex: 'Quantity',
                                renderer: Ext.util.Format.numberRenderer('0,000.000'),
                                hideable: false
                            },
                            {
                                text: '计量单位',
                                width: 100,
                                sortable: false,
                                align: 'left',
                                dataIndex: 'Unit',
                                hideable: false
                            },
                            {
                                text: '估算单价',
                                width: 120,
                                sortable: false,
                                hideable: false,
                                align: 'right',
                                renderer: Ext.util.Format.numberRenderer('0,000.000'),
                                dataIndex: 'Price'
                            },
                            {
                                text: '小计',
                                width: 120,
                                sortable: false,
                                hideable: false,
                                align: 'right',
                                renderer: Ext.util.Format.numberRenderer('0,000.000'),
                                dataIndex: 'Amount'
                            }
                        ],
                        height: 380
                    }
                 );

                //计算最小日期
                var minValue = new Date();

                //计算当天的星期数
                var day = minValue.getDay();

                //移动到下周第一天
                minValue = Ext.Date.add(minValue, Ext.Date.DAY, 7 - day);

                //计算最大日期
                var maxValue = Ext.Date.add(minValue, Ext.Date.DAY, 6);

                //创建窗体
                this.win_create = new Ext.Window
                (
                    {
                        title: '新增养护计划',
                        maximizable: false,
                        maximized: false,
                        width: 800,
                        height: 600,
                        bodyBorder: false,
                        draggable: true,
                        isTopContainer: true,
                        modal: true,
                        resizable: false,
                        bodyPadding: 0,
                        closeAction: 'hide',
                        closable: false,
                        layout: 'fit',
                        listeners:
                        {
                            //挂接显示事件
                            show: function (win, options)
                            {
                                //清除数据
                                store_plans.removeAll(false);
                            }
                        },
                        fieldDefaults:
                        {
                            labelAlign: 'left',
                            labelWidth: 72,
                            anchor: '100%'
                        },
                        items:
                        [
                            {
                                xtype: 'form',
                                method: 'POST',
                                waitMsgTarget: true,
                                waitTitle: Common.ApplicationName,
                                frame: false,
                                bodyPadding: 12,
                                bodyStyle: 'background-color:White;margin:0;',
                                bodyBorder: false,
                                border: 0,
                                autoScroll: true,
                                defaultType: 'textfield',
                                autoScroll: false,
                                fieldDefaults:
                                {
                                    labelAlign: 'left',
                                    labelWidth: 72,
                                    anchor: '100%',
                                    msgTarget: 'qtip',
                                    autoFitErrors: true,
                                    disabledCls: ''
                                },
                                items:
                                [
                                    {
                                        xtype: 'panel',
                                        padding: '0 0 5 0',
                                        border: 0,
                                        layout:
                                        {
                                            type: 'hbox'
                                        },
                                        items:
                                        [
                                            {
                                                xtype: 'datefield',
                                                name: 'ApplicationDate',
                                                fieldLabel: '计划日期',
                                                emptyText: '计划日期',
                                                format: 'Y/m/d',
                                                width: 255,
                                                allowBlank: false,
                                                listeners:
                                                {
                                                    change: function (field, newValue, oldValue, eOpts)
                                                    {
                                                        //判断是否指定了日期
                                                        if (newValue)
                                                        {
                                                            //获得周
                                                            var week = Common.GetWeekOfYear(newValue);

                                                            //获得显示字符串
                                                            var text = newValue.getFullYear() + '年 第' + week + '周';

                                                            //更新文字
                                                            field.up('form').down('#text-week').setValue(text);
                                                        }
                                                        else
                                                        {
                                                            //更新文字
                                                            field.up('form').down('#text-week').setValue();
                                                        }
                                                    }
                                                }
                                            },
                                            {
                                                xtype: 'textfield',
                                                padding: '0 0 0 12',
                                                itemId: 'text-week',
                                                width: 200,
                                                readOnly: true,
                                                value: ''
                                            }
                                        ]
                                    },
                                    {
                                        xtype: 'panel',
                                        padding: '0 0 5 0',
                                        border: 0,
                                        layout:
                                        {
                                            type: 'hbox'
                                        },
                                        items:
                                        [
                                            {
                                                xtype: 'displayfield',
                                                fieldLabel: '养护计划'
                                            },
                                            grid
                                        ]
                                    },
                                    {
                                        xtype: 'textareafield',
                                        name: 'Memo',
                                        fieldLabel: '备　　注',
                                        emptyText: '备注信息（最长200个字符）',
                                        maxLength: 200,
                                        autoScroll: true
                                    }
                                ],
                                buttons:
                                [
                                    {
                                        text: '确定',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up('form').getForm();

                                            //判断是否有养护计划
                                            if (store_plans.getCount() == 0)
                                            {
                                                //提示错误信息
                                                Ext.MessageBox.show
                                                (
                                                    {
                                                        title: Common.ApplicationName,
                                                        msg: '没有指定养护计划，至少需要添加一条养护计划数据。',
                                                        buttons: Ext.MessageBox.OK,
                                                        icon: Ext.Msg.ERROR
                                                    }
                                                );

                                                //返回
                                                return;
                                            }

                                            //用于保存数据
                                            var items = [];

                                            //创建迭代函数
                                            var collect = function (record)
                                            {
                                                //创建数据
                                                var data =
                                                {
                                                    CategoryID: record.data.CategoryID,
                                                    Quantity: record.data.Quantity,
                                                    Price: record.data.Price
                                                };

                                                //添加数据
                                                items.push(data);

                                                //迭代所有数据
                                                return true;
                                            };

                                            //循环添加数据
                                            store_plans.each(collect);

                                            //序列化计划数据
                                            var itemsRaw = Ext.JSON.encode(items);

                                            //检查数据是否合法
                                            if (form.isValid())
                                            {
                                                //提交数据
                                                form.submit
                                                (
                                                    {
                                                        url: Common.MakeAbsoluteUrl('Create'),
                                                        method: HttpMethod.Post,
                                                        params: { ItemsRaw: itemsRaw },
                                                        scope: this.up('window'),
                                                        submitEmptyText: false,
                                                        waitTitle: '请稍候',
                                                        waitMsg: '正在保存数据...',
                                                        success: function (form, action)
                                                        {
                                                            //处理提交错误
                                                            if (Common.HandleFormError(form, action))
                                                            {
                                                                //重置数据
                                                                form.reset();
                                                                //隐藏窗体
                                                                this.hide();
                                                                //刷新数据
                                                                store.load();
                                                            }
                                                        },
                                                        failure: Common.HandleFormFailure
                                                    }
                                                );
                                            }
                                        }
                                    },
                                    {
                                        text: '取消',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up('form').getForm();
                                            //重置数据
                                            form.reset();
                                            //隐藏窗体
                                            this.up('window').hide();
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                );
            }

            //显示窗体
            this.win_create.show();
        },

        //处理编辑按钮点击事件
        onEditClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得表格控件
            var maintainplan_grid = this;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //判定窗体是否创建
                if (!this.win_edit)
                {
                    //创建数据表格
                    var grid = Ext.create
                    (
                        'Ext.grid.Panel',
                        {
                            store: store_plans,
                            flex: 1,
                            refreshSummary: function ()
                            {
                                try
                                {
                                    //获得合计行
                                    var txtSummary = this.down('#text-summary')
                                    //计算合计
                                    var summary = store_plans.sum('Amount', false);
                                    //显示合计值
                                    txtSummary.update('合计：' + Ext.util.Format.number(summary, '0,000.000'));
                                }
                                catch (e)
                                {
                                    alert(e);
                                }
                            },
                            listeners:
                            {
                                //处理选择变化事件
                                selectionchange: function (electionModel, selections, eOpts)
                                {
                                    //判断是否选中养护
                                    if (selections.length === 0)
                                    {
                                        //禁用删除按钮
                                        this.down('#button-delete').setDisabled(true);
                                        //禁用编辑按钮
                                        this.down('#button-edit').setDisabled(true);
                                    }
                                    else
                                    {
                                        //启用删除按钮
                                        this.down('#button-delete').setDisabled(false);
                                        //启用编辑按钮
                                        this.down('#button-edit').setDisabled(false);
                                    }
                                }
                            },
                            dockedItems:
                            [
                                {
                                    xtype: 'toolbar',
                                    dock: 'bottom',
                                    border: 0,
                                    height: 36,
                                    items:
                                    [
                                        {
                                            iconCls: 'icon-add',
                                            itemId: 'button-add',
                                            text: '新增',
                                            scope: this,
                                            hidden: false,
                                            tooltip: '新增养护计划',
                                            handler: function (button)
                                            {
                                                //获得表格控件
                                                var grid = button.up('grid');

                                                //新增记录
                                                maintainplan_grid.onAppendPlanItem(grid);
                                            }
                                        },
                                        {
                                            iconCls: 'icon-edit',
                                            itemId: 'button-edit',
                                            text: '编辑',
                                            scope: this,
                                            hidden: false,
                                            disabled: true,
                                            tooltip: '编辑养护计划',
                                            handler: function (button)
                                            {
                                                //获得表格控件
                                                var grid = button.up('grid');

                                                //获得选中的行
                                                var selection = grid.getView().getSelectionModel().getSelection();

                                                //判断是否选中行
                                                if (selection.length > 0)
                                                {
                                                    //编辑记录
                                                    maintainplan_grid.onEditPlanItem(grid, selection[0]);
                                                }
                                            }
                                        },
                                        {
                                            iconCls: 'icon-delete',
                                            itemId: 'button-delete',
                                            text: '删除',
                                            scope: this,
                                            hidden: false,
                                            disabled: true,
                                            tooltip: '删除养护计划',
                                            handler: function (button)
                                            {
                                                //获得表格控件
                                                var grid = button.up('grid');

                                                //获得选中的行
                                                var selection = grid.getView().getSelectionModel().getSelection();

                                                //判断是否选中行
                                                if (selection.length > 0)
                                                {
                                                    //删除数据
                                                    store_plans.remove(selection);
                                                }

                                                //刷新合计
                                                grid.refreshSummary();
                                            }
                                        },
                                        '->',
                                        {
                                            itemId: 'text-summary',
                                            xtype: 'tbtext',
                                            text: '合计：0.000'
                                        }
                                    ]
                                }
                            ],
                            columns:
                            [
                                {
                                    text: '养护类型',
                                    width: 160,
                                    flex: true,
                                    sortable: false,
                                    align: 'left',
                                    dataIndex: 'Category',
                                    hideable: false
                                },
                                {
                                    text: '养护工程量',
                                    width: 120,
                                    sortable: false,
                                    align: 'right',
                                    dataIndex: 'Quantity',
                                    renderer: Ext.util.Format.numberRenderer('0,000.000'),
                                    hideable: false
                                },
                                {
                                    text: '计量单位',
                                    width: 100,
                                    sortable: false,
                                    align: 'left',
                                    dataIndex: 'Unit',
                                    hideable: false
                                },
                                {
                                    text: '估算单价',
                                    width: 120,
                                    sortable: false,
                                    hideable: false,
                                    align: 'right',
                                    renderer: Ext.util.Format.numberRenderer('0,000.000'),
                                    dataIndex: 'Price'
                                },
                                {
                                    text: '小计',
                                    width: 120,
                                    sortable: false,
                                    hideable: false,
                                    align: 'right',
                                    renderer: Ext.util.Format.numberRenderer('0,000.000'),
                                    dataIndex: 'Amount'
                                }
                            ],
                            height: 380
                        }
                     );

                    //创建窗体
                    this.win_edit = new Ext.Window
                    (
                        {
                            title: '编辑养护计划',
                            maximizable: false,
                            maximized: false,
                            width: 800,
                            height: 600,
                            bodyBorder: false,
                            draggable: true,
                            isTopContainer: true,
                            modal: true,
                            resizable: false,
                            bodyPadding: 0,
                            closeAction: 'hide',
                            closable: false,
                            layout: 'fit',
                            listeners:
                            {
                                //挂接显示事件
                                show: function (win, options)
                                {
                                    //获得表单
                                    var form = win.down('form');

                                    //设置开始加载
                                    form.setLoading(true);

                                    //清除数据
                                    store_plans.removeAll(false);

                                    //创建请求对象
                                    Ext.Ajax.request
                                    (
                                        {
                                            url: Common.MakeControllerUrl('MaintainPlan', 'Details?id=' + win.current_row.PlanID),
                                            method: HttpMethod.Post,
                                            callback: function (options, success, response)
                                            {
                                                try
                                                {
                                                    //判定是否发生错误
                                                    if (!success && options && options.failure)
                                                    {
                                                        //抛出异常
                                                        throw '数据加载失败';
                                                    }

                                                    //判定是否有反馈对象
                                                    if (!response)
                                                    {
                                                        //抛出异常
                                                        throw '无法读取响应数据';
                                                    }

                                                    //用于保存反馈对象
                                                    var responseData = {};

                                                    //解析反馈数据
                                                    responseData = Ext.JSON.decode(response.responseText, true);

                                                    //判断是否解析成功
                                                    if (!responseData)
                                                    {
                                                        //抛出异常
                                                        throw response.responseText;
                                                    }
                                                    //判定是否称成功
                                                    if (!responseData.success)
                                                    {
                                                        //抛出异常
                                                        throw responseData.message;
                                                    }

                                                    //获得表单
                                                    var formBase = form.getForm();

                                                    //读取数据
                                                    formBase.setValues(responseData.data);

                                                    //加载数据
                                                    store_plans.loadData(responseData.data.Items);

                                                    //刷新合计
                                                    grid.refreshSummary();
                                                }
                                                catch (e)
                                                {
                                                    //提示错误信息
                                                    Ext.MessageBox.show
                                                    (
                                                        {
                                                            title: Common.ApplicationName,
                                                            msg: e,
                                                            buttons: Ext.MessageBox.OK,
                                                            icon: Ext.Msg.ERROR
                                                        }
                                                    );

                                                    //设置停止加载
                                                    form.setLoading(false);

                                                    //隐藏窗体
                                                    win.hide();
                                                }

                                                //设置结束加载
                                                form.setLoading(false);
                                            }
                                        }
                                    );
                                }
                            },
                            fieldDefaults:
                            {
                                labelAlign: 'left',
                                labelWidth: 72,
                                anchor: '100%'
                            },
                            items:
                            [
                                {
                                    xtype: 'form',
                                    method: 'POST',
                                    waitMsgTarget: true,
                                    waitTitle: Common.ApplicationName,
                                    frame: false,
                                    bodyPadding: 12,
                                    bodyStyle: 'background-color:White;margin:0;',
                                    bodyBorder: false,
                                    border: 0,
                                    autoScroll: true,
                                    defaultType: 'textfield',
                                    autoScroll: false,
                                    fieldDefaults:
                                    {
                                        labelAlign: 'left',
                                        labelWidth: 72,
                                        anchor: '100%',
                                        msgTarget: 'qtip',
                                        autoFitErrors: true,
                                        disabledCls: ''
                                    },
                                    items:
                                    [
                                        {
                                            xtype: 'hiddenfield',
                                            itemId: 'txtPlanID',
                                            name: 'PlanID'
                                        },
                                        {
                                            xtype: 'panel',
                                            padding: '0 0 5 0',
                                            border: 0,
                                            layout:
                                            {
                                                type: 'hbox'
                                            },
                                            items:
                                            [
                                                {
                                                    xtype: 'datefield',
                                                    name: 'ApplicationDate',
                                                    fieldLabel: '计划日期',
                                                    emptyText: '计划日期',
                                                    format: 'Y/m/d',
                                                    width: 255,
                                                    allowBlank: false,
                                                    listeners:
                                                    {
                                                        change: function (field, newValue, oldValue, eOpts)
                                                        {
                                                            //判断是否指定了日期
                                                            if (newValue)
                                                            {
                                                                //获得周
                                                                var week = Common.GetWeekOfYear(newValue);

                                                                //获得显示字符串
                                                                var text = newValue.getFullYear() + '年 第' + week + '周';

                                                                //更新文字
                                                                field.up('form').down('#text-week').setValue(text);
                                                            }
                                                            else
                                                            {
                                                                //更新文字
                                                                field.up('form').down('#text-week').setValue();
                                                            }
                                                        }
                                                    }
                                                },
                                                {
                                                    xtype: 'textfield',
                                                    padding: '0 0 0 12',
                                                    itemId: 'text-week',
                                                    width: 200,
                                                    readOnly: true,
                                                    value: ''
                                                }
                                            ]
                                        },
                                        {
                                            xtype: 'panel',
                                            padding: '0 0 5 0',
                                            border: 0,
                                            layout:
                                            {
                                                type: 'hbox'
                                            },
                                            items:
                                            [
                                                {
                                                    xtype: 'displayfield',
                                                    fieldLabel: '养护计划'
                                                },
                                                grid
                                            ]
                                        },
                                        {
                                            xtype: 'textareafield',
                                            name: 'Memo',
                                            fieldLabel: '备　　注',
                                            emptyText: '备注信息（最长200个字符）',
                                            maxLength: 200,
                                            autoScroll: true
                                        }
                                    ],
                                    buttons:
                                    [
                                        {
                                            text: '确定',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();

                                                //判断是否有养护计划
                                                if (store_plans.getCount() == 0)
                                                {
                                                    //提示错误信息
                                                    Ext.MessageBox.show
                                                    (
                                                        {
                                                            title: Common.ApplicationName,
                                                            msg: '没有指定养护计划，至少需要添加一条养护计划数据。',
                                                            buttons: Ext.MessageBox.OK,
                                                            icon: Ext.Msg.ERROR
                                                        }
                                                    );

                                                    //返回
                                                    return;
                                                }

                                                //用于保存数据
                                                var items = [];

                                                //创建迭代函数
                                                var collect = function (record)
                                                {
                                                    //创建数据
                                                    var data =
                                                    {
                                                        CategoryID: record.data.CategoryID,
                                                        Quantity: record.data.Quantity,
                                                        Price: record.data.Price
                                                    };

                                                    //添加数据
                                                    items.push(data);

                                                    //迭代所有数据
                                                    return true;
                                                };

                                                //循环添加数据
                                                store_plans.each(collect);

                                                //序列化计划数据
                                                var itemsRaw = Ext.JSON.encode(items);

                                                //检查数据是否合法
                                                if (form.isValid())
                                                {
                                                    //提交数据
                                                    form.submit
                                                    (
                                                        {
                                                            url: Common.MakeAbsoluteUrl('Update'),
                                                            method: HttpMethod.Post,
                                                            params: { ItemsRaw: itemsRaw },
                                                            scope: this.up('window'),
                                                            submitEmptyText: false,
                                                            waitTitle: '请稍候',
                                                            waitMsg: '正在保存数据...',
                                                            success: function (form, action)
                                                            {
                                                                //处理提交错误
                                                                if (Common.HandleFormError(form, action))
                                                                {
                                                                    //重置数据
                                                                    form.reset();
                                                                    //隐藏窗体
                                                                    this.hide();
                                                                    //刷新数据
                                                                    store.load();
                                                                }
                                                            },
                                                            failure: Common.HandleFormFailure
                                                        }
                                                    );
                                                }
                                            }
                                        },
                                        {
                                            text: '取消',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();
                                                //重置数据
                                                form.reset();
                                                //隐藏窗体
                                                this.up('window').hide();
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    );
                }

                //设置当前行
                this.win_edit.current_row = selection.data;

                //显示窗体
                this.win_edit.show();
            }
        },

        //处理提交按钮点击事件
        onSubmitClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {

                //记录所有的编号
                var plans = [];

                //循环添加
                for (var index in selection)
                {
                    //添加记录
                    plans.push(selection[index].data.PlanID);
                }

                //询问是否确定删除
                Ext.Msg.show
                (
                    {
                        title: '提交审核',
                        width: 320,
                        height: 240,
                        msg: '确定要将选中的养护计划提交审核吗?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('ExecuteSubmit'),
                                        method: HttpMethod.Post,
                                        params:
                                        {
                                            plans: plans, passed: true
                                        },
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //加载数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', { path: _Current_ActionUrl }));

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建养护类型存储
        store_master_category = Ext.create
        (
            'Ext.data.Store',
            {
                fields: ['ID', 'Name', 'Unit'],
                data: master_category
            }
        );

        //创建计划存储
        store_plans = Ext.create
        (
            'Ext.data.ArrayStore',
            {
                autoDestroy: true,
                model: 'MaintainPlanItemModel'
            }
        );

        //创建数据存储对象
        var store = Ext.create
        (
            'Ext.data.Store',
            {
                pageSize: 20,
                autoSync: true,
                autoLoad: true,
                root: 'data',
                remoteFilter: true,
                remoteSort: true,
                model: 'MaintainPlanModel',
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    url: Common.MakeAbsoluteUrl('SubmitSelect'),
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function (proxy, response, operation, options)
                        {
                            //处理错误
                            Common.HandleProxyError(proxy, response, operation, options);
                        }
                    }
                }
            }
        );

        //创建选择模型
        var selectionModel = Ext.create('Ext.selection.CheckboxModel');

        //创建表格
        Ext.create
        (
            'MaintainPlan.Grid',
            {
                store: store,
                selModel: selectionModel,
                stateful: true,
                stateId: 'maintainplan-submit-state-grid',
                height: 600,
                width: '100%',
                id: 'maintainplan-grid',
                title: '',
                renderTo: 'data-grid',
                columnLines: true,
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                //创建分页工具栏
                bbar: Ext.create
                (
                    'Ext.PagingToolbar',
                    {
                        store: store,
                        displayInfo: true,
                        displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                        emptyMsg: ''
                    }
                ),
                columns:
                [
                    {
                        text: '单位名称',
                        width: 160,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Organization',
                        hideable: false
                    },
                    {
                        text: '状态',
                        width: 60,
                        sortable: true,
                        align: 'center',
                        dataIndex: 'State',
                        renderer: Common.RenderMaintainDataState
                    },
                    {
                        text: '审核意见',
                        xtype: 'templatecolumn',
                        tpl: '<a class="Comment" href="javascript:void(0);" onclick="javascript:return Common.ViewComment(this);" title="{Comment}">{Comment}</a> ',
                        width: 80,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'Comment',
                        hideable: true
                    },
                    {
                        text: '计划日期',
                        width: 120,
                        sortable: true,
                        align: 'center',
                        renderer: Common.RenderWeekOfYear,
                        dataIndex: 'ApplicationDate'
                    },
                    {
                        text: '计划额',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.000'),
                        dataIndex: 'Amount'
                    },
                    {
                        text: '填报人',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Applicant'
                    },
                    {
                        text: '填报日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'CreateDate'
                    },
                    {
                        text: '延期填报',
                        width: 80,
                        sortable: true,
                        align: 'center',
                        renderer: renderGridReadonlyCheckBox,
                        dataIndex: 'IsDelay'
                    },
                    {
                        text: '备注',
                        xtype: 'templatecolumn',
                        tpl: '<span title="{Memo}">{Memo}</span> ',
                        width: 160,
                        minWidth: 160,
                        flex: 1,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'Memo',
                        hideable: true
                    }
                ]
            }
        );

        $(window).bind
        (
            "resize",
            function ()
            {
                //调整表格宽度
                Common.AdjustGridWidth('maintainplan-grid', 'body_content');
            }
        );

        //调整表格宽度
        Common.AdjustGridWidth('maintainplan-grid', 'body_content');
    }
);
